package me.notelife.svnmonitor;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import me.notelife.svnmonitor.domain.VCSLogEntry;
import me.notelife.svnmonitor.service.VCSLogService;

import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

public class History {

    public static void main(String[] args) {

        String url = "x";
        String name = "x";
        String password = "x";
        long startRevision = 225468;
        long endRevision = -1;//HEAD (the latest) revision

        DAVRepositoryFactory.setup();

        SVNRepository repository = null;
        
        try {
            repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
        } catch (SVNException svne) {
            System.err
                    .println("error while creating an SVNRepository for the location '"
                            + url + "': " + svne.getMessage());
            System.exit(1);
        }

        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password);
        repository.setAuthenticationManager(authManager);

        try {
            endRevision = repository.getLatestRevision();
        } catch (SVNException svne) {
            System.err.println("error while fetching the latest repository revision: " + svne.getMessage());
            System.exit(1);
        }

        Collection logEntries = null;
        try {

            logEntries = repository.log(new String[] {""}, null,
                    startRevision, endRevision, true, true);

        } catch (SVNException svne) {
            System.out.println("error while collecting log information for '"
                    + url + "': " + svne.getMessage());
            System.exit(1);
        }
        for (Iterator entries = logEntries.iterator(); entries.hasNext();) {

            SVNLogEntry logEntry = (SVNLogEntry) entries.next();

            StringBuilder changes = new StringBuilder();
            if (logEntry.getChangedPaths().size() > 0) {
                System.out.println();
                System.out.println("changed paths:");
                /*
                 * keys are changed paths
                 */
                Set changedPathsSet = logEntry.getChangedPaths().keySet();

                for (Iterator changedPaths = changedPathsSet.iterator(); changedPaths
                        .hasNext();) {

                    SVNLogEntryPath entryPath = (SVNLogEntryPath) logEntry
                            .getChangedPaths().get(changedPaths.next());

                    changes.append( " "
                            + entryPath.getType()
                            + "	"
                            + entryPath.getPath()
                            + ((entryPath.getCopyPath() != null) ? " (from "
                                    + entryPath.getCopyPath() + " revision "
 +
                            entryPath.getCopyRevision() + ")" : "\n" ) );
                }
            }

            System.out.println( "---------------------------------------------" );
            /*
             * gets the revision number
             */
            System.out.println( "revision: " + logEntry.getRevision() );
            /*
             * gets the author of the changes made in that revision
             */
            System.out.println( "author: " + logEntry.getAuthor() );
            /*
             * gets the time moment when the changes were committed
             */
            System.out.println( "date: " + logEntry.getDate() );
            /*
             * gets the commit log message
             */
            System.out.println( "log message: " + logEntry.getMessage() );

            System.out.println( changes.toString() );

            VCSLogService logSvc = new VCSLogService();

            VCSLogEntry entry = new VCSLogEntry();
            entry.setRevision( logEntry.getRevision() );
            entry.setSubmitter( logEntry.getAuthor() );
            entry.setTimestamp( new Timestamp( logEntry.getDate().getTime() ) );
            entry.setComments( logEntry.getMessage() );
            entry.setChanges( changes.toString() );

            try
            {
                logSvc.createVCSLogEntry( entry );
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
        }
    }

}